package cn.com.huzo.repository.master.impl;

import cn.com.huzo.model.entities.master.MaterialEntity;
import huzofw.repository.impl.GenericRepositoryImpl;
import huzofw.repository.impl.MyBatisDaoUtils;
import cn.com.huzo.repository.master.MaterialRepository;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;

/**
 * 材料信息数据访问对象。
 *
 * @author 吴旭
 */
@Repository("MaterialRepository")
public class MaterialRepositoryImpl extends GenericRepositoryImpl<MaterialEntity, Long>
        implements MaterialRepository {

    private final String namespace = "cn.com.huzo.repository.master.MaterialRepository";

    /**
     * 构造函数,设置the instance to MaterialRepositoryImpl.class.
     */
    public MaterialRepositoryImpl() {
        super(MaterialEntity.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public MaterialEntity fetchByKey(String rootNodeName, Long materialId, Long currentUserId, Long workFlowId) {
        Map<String, Object> criterion = new HashMap<>();
        criterion.put("RootNodeName", rootNodeName);
        criterion.put("MaterialId", materialId);
        criterion.put("CurrentUserId", currentUserId);
        criterion.put("WorkFlowId", workFlowId);

        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchByKey"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public MaterialEntity fetchByMaterialId(Long materialId) {
        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchByMaterialId"), materialId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer fetchForCount(Map<String, Object> criterion) {
        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchForCount"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<MaterialEntity> fetchForPage(Map<String, Object> criterion) {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchForPage"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Long insert(MaterialEntity instance) {
        // 影响的行数
        int rowsAffected = getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                namespace, "insert"), instance);

        // 返回出入后的记录ID
        return instance.getMaterialId();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer update(MaterialEntity instance) {
        return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                namespace, "update"), instance);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer delete(Long key) {
        return getSqlSession().delete(MyBatisDaoUtils.getMapperQueryId(
                namespace, "delete"), key);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer batchChangeCatalogue(List<MaterialEntity> itemList) {
        return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                namespace, "batchChangeCatalogue"), itemList);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer importDataFromCsv(MaterialEntity instance) {
        return getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                namespace, "importDataFromCsv"), instance);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<MaterialEntity> fetchAllFields() {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchAllFields"));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<String> fetchExportData(Map<String, Object> criterion) {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchExportData"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer doCheck(Long materialId, Long workFlowId, Short nodeOrder, Long checkedBy, Short isCheckCurrentNode) {
        Map<String, Object> criterion = new HashMap();
        criterion.put("MaterialId", materialId);
        criterion.put("WorkFlowId", workFlowId);
        criterion.put("NodeOrder", nodeOrder);
        criterion.put("CheckedBy", checkedBy);
        criterion.put("IsCheckCurrentNode", isCheckCurrentNode);

        return getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                namespace, "check"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer doUnCheck(Long materialId, Short nodeOrder, Long checkedBy) {
        Map<String, Object> criterion = new HashMap();
        criterion.put("MaterialId", materialId);
        criterion.put("NodeOrder", nodeOrder);
        criterion.put("CheckedBy", checkedBy);

        return getSqlSession().delete(MyBatisDaoUtils.getMapperQueryId(
                namespace, "unCheck"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<String> fetchMaterialCodeForAutoComplate(String keyWord) {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchMaterialCodeForAutoComplate"), keyWord);
    }
}
